package com.cqxyd.smartsite.util;

import java.security.MessageDigest;

public class MD5Helper {
	/*
	 * 对字符串进行MD5加密
	 * 
	 */
	public static String MD5Encrypt(String key) {
		String result = "";
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(key.getBytes("utf-8"));
			byte[] resultByteArray = md5.digest();
			result = byteArrayToHex(resultByteArray);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String byteArrayToHex(byte[] byteArray) {
		char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		char[] resultCharArray = new char[byteArray.length * 2];
		int index = 0;
		for (byte b : byteArray) {
			resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
			resultCharArray[index++] = hexDigits[b & 0xf];
		}
		return new String(resultCharArray);
	}

}
